import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
import qs from 'qs'

const upms = CommonConfig.upmsUrl + '/v1'
const bms = CommonConfig.bmsUrl + '/v1'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

/**
 * 列举菜单
 * @param clientCode 客户端类型 code
 * @returns {Promise<unknown>}
 * 弃用未删除
 */
function listMenus (clientCode) {
  const url = upms + '/menu_list_flat'

  let config = {
    params: {
      terminalType: clientCode
    }
  }

  return axiosInstance.get(url, config).then(resp => {
    return Promise.resolve(resp)
  })
}

/**
 * 列举角色菜单
 * @param roleId 角色 ID
 * @param clientCode 客户端类型 code
 * 弃用未删除
 */
function listRoleMenus (roleId, clientCode) {
  const url = upms + '/rolemenu_list'

  let config = {
    params: {
      roleId,
      terminalType: clientCode
    }
  }

  return axiosInstance.get(url, config).then(resp => {
    return Promise.resolve(resp)
  })
}

/**
 * 添加角色菜单权限
 * @param roleId 权限 ID
 * @param menuIds 菜单 ID 数组
 * @returns {Promise<unknown>}
 * 弃用未删除
 */
function addRoleMenus (roleId, menuIds) {
  const url = upms + '/rolemenu'

  let data = {
    roleId,
    menuIds
  }

  return axiosInstance.post(url, data).then(resp => {
    return Promise.resolve(resp)
  })
}

/**
 * 删除角色菜单权限
 * @param roleId 权限 ID
 * @param menuIds 菜单 ID 数组
 * @returns {Promise<unknown>}
 * 弃用未删除
 */
function deleteRoleMenus (roleId, menuIds) {
  const url = upms + '/rolemenu'

  let config = {
    params: {
      roleId,
      menuIds
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }

  return axiosInstance.delete(url, config).then(resp => {
    return Promise.resolve(resp)
  })
}

/**
 * 列举角色权限映射数组
 * @param roleId 角色Id列表
 * @param clientType 设备类型
 * @returns {Promise<unknown>}
 * sirio：2020/2/12
 * **/
function getPermissions (roleId, clientType) {
  const newUrl = `${upms}/roles/${roleId}/view_privileges`

  let reqData = {
    params: {
      clientType: clientType
    }
  }

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 列举租户全部拥有的视图权限Id
 * @param companyId 租户id
 * @param clientType 设备类型
 * @returns {Promise<unknown>}
 * sirio：2020/2/13
 * **/
function getCompanyView (companyId, clientType) {
  const newUrl = `${bms}/companies/${companyId}/view_privileges`

  let reqData = {
    params: {
      clientType: clientType
    }
  }

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 保存角色全部拥有的视图权限Id
 * @param roleId 角色id
 * @param clientType 设备类型
 * @returns {Promise<unknown>}
 * sirio：2020/2/13
 * **/
function savePermission (roleId, clientType, privileges) {
  const url = upms + `/roles/${roleId}/view_privileges`

  let reqData = {
    clientType,
    privileges
  }

  return axiosInstance.put(url, reqData).then(resp => {
    return Promise.resolve(resp)
  })
}

/**
 * 保存角色全部拥有的业务权限Id
 * @param roleId 角色id
 * @param clientType 设备类型
 * @returns {Promise<unknown>}
 * sirio：2020/2/13
 * **/
function getBusinessPermission (roleId, clientType) {
  const url = upms + `/roles/${roleId}/biz_privileges`

  let reqData = {
    params: {
      clientType: clientType
    }
  }

  return axiosInstance.get(url, reqData).then(resp => {
    return Promise.resolve(resp)
  })
}

/**
 * 保存角色全部拥有的业务权限Id
 * @param roleId 角色id
 * @param clientType 设备类型
 * @returns {Promise<unknown>}
 * sirio：2020/2/13
 * **/
function saveBusinessPermission (roleId, clientType, privileges) {
  const url = upms + `/roles/${roleId}/biz_privileges`

  let reqData = {
    clientType: clientType,
    privileges: privileges
  }

  return axiosInstance.put(url, reqData).then(resp => {
    return Promise.resolve(resp)
  })
}
// 保存角色全部拥有的业务权限 单个
export function saveBusinessPermissionItem (roleId, clientType, privileges) {
  const url = upms + `/roles/${roleId}/biz_privilege`

  let reqData = {
    clientType: clientType,
    privileges: [privileges]
  }
  return axiosInstance.put(url, reqData).then(resp => {
    return Promise.resolve(resp)
  })
}

// /v1/role_data_privilege/role/{roleId}
// 获取角色视图权限
export function getRoleData (roleId, clientType) {
  const url = upms + `/role_data_privilege/role/${roleId}?clientType=${clientType}`
  return axiosInstance.get(url).then(resp => {
    return Promise.resolve(resp)
  })
}
// POST
// /v1/role_data_privilege/{roleId}/{viewPrivilegeId}
// 保存角色数据权限
export function postPrivilege (val) {
  const url = upms + `/role_data_privilege/${val.roleId}/${val.viewPrivilegeId}?clientType=${val.clientType}&isAll=${val.isAll}`
  const params = val.data
  return axiosInstance.post(url, params).then(resp => {
    return Promise.resolve(resp)
  })
}
export default {
  listMenus,
  listRoleMenus,
  addRoleMenus,
  deleteRoleMenus,
  getPermissions,
  getCompanyView,
  savePermission,
  getBusinessPermission,
  saveBusinessPermission
}
